package de.davidneu.gpxviewer.speedprofile
{
	import de.davidneu.gpx.GpxTrack;
	import de.davidneu.gpxviewer.heightProfile.HeightProfile;
	import de.davidneu.gpxviewer.heightProfile.HeightProfileEvent;
	import de.davidneu.gpxviewer.heightProfile.ProfileDistanceRenderer;
	import de.davidneu.gpxviewer.heightProfile.ProfileLinesRenderer;
	import de.davidneu.gpxviewer.heightProfile.ProfileRenderer;
	import de.davidneu.gpxviewer.heightProfile.ProfileTimeRenderer;

	/**
	 * @author David Neu
	 */
	public class SpeedProfile extends HeightProfile
	{
        [Bindable]
    	public var minimumSpeed:Number = 0;
        
        [Bindable]
        public var maximumSpeed:Number = 0;
        
		public function SpeedProfile()
		{
			super();
		}
		
        override public function set track(track : GpxTrack) : void
        {
            _track = track;
            var speed:Number = track.maxSpeed - track.minSpeed;
            var spacing:Number = speed * DEFAULT_SPACING;
            
            if (minimumSpeed == 0 && maximumSpeed == 0) {
                minimumSpeed = Math.round(track.minSpeed - spacing);
                maximumSpeed = Math.round(track.maxSpeed + spacing);
                dispatchEvent(new HeightProfileEvent(HeightProfileEvent.CHANGE_ELEVATION));
            }
        }
        
        override public function render():void
        {
        	var lineThickness:Number = 2;
        	var lineAlpha:Number = 1.5;
        	var bgColor:Number = 0x333333;
        	var bgAlpha:Number = .2;
        	
            if (_lines != null) {
            	removeChild(_lines);
            	_lines = null;
            }
            _lines = new ProfileLinesRenderer(_width, _height, maximumSpeed, minimumSpeed, 10, 0xCCCCCC, 1, 1, true);
            _lines.render();
            addChild(_lines);
        	
        	if (_profile != null) {
        		removeChild(_profile);
        		_profile = null;
        	}
        	
            _profile = new ProfileSpeedRenderer(_width, _height, _track, maximumSpeed, minimumSpeed, lineColor, lineThickness, lineAlpha, bgColor, bgAlpha);
			_profile.colorBySpeed = _colorBySpeed;
			_profile.type = _type;
            addChild(_profile);
            _profile.render();
            
            if (_distances != null) {
                removeChild(_distances);
                _distances = null;
            }
            if (_type == ProfileRenderer.MODE_TIME) {
                _distances = new ProfileTimeRenderer(_width, _height, 0, _track.time, 10);
            } else {
                _distances = new ProfileDistanceRenderer(_width, _height, 0, _track.distance, 10);
            }
            _distances.render();
            addChild(_distances);
            onMouseMove(null);
        }
	}
}
